package com.travel.server.manager.impl.command.user;

import com.travel.server.manager.dto.request.user.QueryUserDetailRequest;
import com.travel.server.manager.dto.response.user.QueryUserDetailResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.DepartmentDal;
import com.travel.server.manager.impl.dal.UserDal;
import com.travel.server.manager.model.DepartmentInfo;
import com.travel.server.manager.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/20 12:53
 * @Desc:
 */
@Component
public class QueryUserDetailCommand extends AbstractCommand<QueryUserDetailRequest, QueryUserDetailResponse> {

    @Autowired
    private UserDal userDal;

    @Autowired
    private DepartmentDal departmentDal;

    @Override
    protected void checkInput(QueryUserDetailRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryUserDetailResponse handle(QueryUserDetailRequest request) {
        checkInput(request);

        UserInfo userInfo = userDal.selectById(request.getId());
        DepartmentInfo departmentInfo = departmentDal.selectById(userInfo.getDepartmentId());

        return makeResponse(userInfo,departmentInfo);
    }

    private QueryUserDetailResponse makeResponse(UserInfo userInfo, DepartmentInfo departmentInfo){
        QueryUserDetailResponse response = new QueryUserDetailResponse();
        response.setAccount(userInfo.getAccount());
        response.setDepartmentId(departmentInfo.getId());
        response.setDepartmentName(departmentInfo.getDepartmentName());
        response.setMobile(userInfo.getMobile());
        response.setName(userInfo.getName());
        response.setState(userInfo.getEnable());
        response.setHeadUrl(userInfo.getHeadUrl());
        return response;
    }
}
